package com.fjh.service;

import com.fjh.pojo.Student;
import com.fjh.pojo.Teacher;
import com.fjh.pojo.TeachingClass;
import com.fjh.repository.StudentRepository;
import com.fjh.repository.TeacherRepository;
import com.fjh.repository.TeachingClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class TeachingClassService {

    @Autowired
    private TeachingClassRepository teachingClassRepository;

    @Autowired
    private TeacherRepository teacherRepository;

    @Autowired
    private StudentRepository studentRepository;

    public TeachingClass createTeachingClass(TeachingClass teachingClass) {
        if (teachingClass.getActivity() == null) {
            throw new IllegalArgumentException("Activity must not be null");
        }

        teachingClass.setCreatedTime(LocalDateTime.now());
        teachingClass.setId(null);

        Teacher teacher = teachingClass.getTeacher();
        if (teacher != null) {
            Long teacherId = teacher.getId();
            if (teacherId != null) {
                Optional<Teacher> savedTeacher = teacherRepository.findById(teacherId);
                if (savedTeacher.isPresent()) {
                    Teacher completeTeacher = savedTeacher.get();
                    teachingClass.setTeacher(completeTeacher);
                } else {
                    throw new IllegalArgumentException("Teacher not found in the database");
                }
            } else {
                throw new IllegalArgumentException("Teacher ID must not be null");
            }
        } else {
            throw new IllegalArgumentException("Teacher must not be null");
        }

        List<Student> students = teachingClass.getStudents();
        if (students != null && !students.isEmpty()) {
            List<Student> completeStudents = new ArrayList<>();
            for (Student student : students) {
                Long studentId = student.getId();
                if (studentId != null) {
                    Optional<Student> savedStudent = studentRepository.findById(studentId);
                    if (savedStudent.isPresent()) {
                        completeStudents.add(savedStudent.get());
                    } else {
                        throw new IllegalArgumentException("Student not found in the database");
                    }
                } else {
                    throw new IllegalArgumentException("Student ID must not be null");
                }
            }
            teachingClass.setStudents(completeStudents);
        }

        return teachingClassRepository.save(teachingClass);
    }


    public TeachingClass addStudentsToClass(Long classId, List<Student> students) {
        Optional<TeachingClass> optionalClass = teachingClassRepository.findById(classId);
        if (optionalClass.isPresent()) {
            TeachingClass teachingClass = optionalClass.get();

            List<Student> completeStudents = new ArrayList<>();
            for (Student student : students) {
                Long studentId = student.getId();
                Optional<Student> savedStudent = studentRepository.findById(studentId);
                if (savedStudent.isPresent()) {
                    completeStudents.add(savedStudent.get());
                } else {
                    throw new IllegalArgumentException("Student not found in the database");
                }
            }

            teachingClass.getStudents().addAll(completeStudents);
            return teachingClassRepository.save(teachingClass);
        } else {
            throw new IllegalArgumentException("TeachingClass not found");
        }
    }


    public TeachingClass assignTeacherToClass(Long classId, List<Teacher> teachers) {
        Optional<TeachingClass> optionalClass = teachingClassRepository.findById(classId);
        if (optionalClass.isPresent()) {
            TeachingClass teachingClass = optionalClass.get();

            List<Teacher> completeTeachers = new ArrayList<>();
            for (Teacher teacher : teachers) {
                Long teacherId = teacher.getId();
                Optional<Teacher> savedTeacher = teacherRepository.findById(teacherId);
                if (savedTeacher.isPresent()) {
                    completeTeachers.add(savedTeacher.get());
                } else {
                    throw new IllegalArgumentException("Teacher not found in the database");
                }
            }

            if (completeTeachers.size() <= 2) {
                teachingClass.setTeachers(completeTeachers);
                return teachingClassRepository.save(teachingClass);
            } else {
                throw new IllegalArgumentException("A TeachingClass can have at most two teachers");
            }
        } else {
            throw new IllegalArgumentException("TeachingClass not found");
        }
    }
}


